package guda.mrpc.example.client;

import guda.mrpc.core.RequestKey;
import guda.mrpc.core.client.ConnectionFactory;
import guda.mrpc.core.client.RpcClient;
import guda.mrpc.core.client.loadblance.impl.RoundLoadBalance;
import guda.mrpc.core.common.SubscribeEnum;
import guda.mrpc.core.config.ClientConfig;
import guda.mrpc.core.registry.ServiceMeta;
import guda.mrpc.core.registry.SubscribeListener;
import guda.mrpc.core.registry.impl.ZookeeperRegistry;
import guda.mrpc.core.serialize.impl.DefaultSerialize;
import guda.mrpc.example.server.service.HelloService;
import guda.mrpc.example.server.service.ReqDTO;
import guda.mrpc.example.server.service.RespDTO;

/**
 * Created by well on 17/5/24.
 */
public class ClientStartTest {

    public static void main(String[] args){
        try {
            System.out.println("3:");
            ClientConfig clientConfig = new ClientConfig();
            clientConfig.setAppId("node");
            clientConfig.setObjectSerialize(new DefaultSerialize());
            clientConfig.setLoadBalance(new RoundLoadBalance());
            ZookeeperRegistry zookeeperRegistry = new ZookeeperRegistry();
            clientConfig.setServiceRegistry(zookeeperRegistry);
            System.out.println("4:");
            RpcClient rpcClient = new RpcClient();
            rpcClient.setClientConfig(clientConfig);
            rpcClient.bindReference(HelloService.class);

            zookeeperRegistry.connectToRegistryServer("127.0.0.1:2181");
            RequestKey requestKey = new RequestKey(HelloService.class.getName());
            zookeeperRegistry.subscribe(requestKey, new SubscribeListener() {
                @Override
                public void subscribe(ServiceMeta serviceMeta,SubscribeEnum subscribeEnum) {
                    if(subscribeEnum == SubscribeEnum.CHILD_ADDED){
                        ConnectionFactory.instance().connect(serviceMeta);
                    }else if(subscribeEnum == SubscribeEnum.CHILD_REMOVED){
                        ConnectionFactory.instance().remove(serviceMeta);
                    }

                }
            });
            zookeeperRegistry.lookup(requestKey);
            System.out.println("1:");
            HelloService proxyBean = rpcClient.getProxyBean(HelloService.class);
            try {
                String abc = proxyBean.say("abc");
                System.out.println("response:" + abc);
            }catch(Exception e){

            }

            Thread.sleep(3000);

            String abc = proxyBean.say("abc2000");
            System.out.println("response:" + abc);

            Thread.sleep(10000);
             abc = proxyBean.say("abc");
            System.out.println("response:" + abc);
//
//            Thread.sleep(2000);
//            ReqDTO reqDTO=new ReqDTO();
//            reqDTO.setName("tom");
//            reqDTO.setAge(13);
//            RespDTO say = proxyBean.say(reqDTO);
//            System.out.println("response:" + say.getCard());



//            Thread.sleep(10000);
//            ReqDTO reqDTO=new ReqDTO();
//            reqDTO.setName("test hearbeat");
//            reqDTO.setAge(13);
//            RespDTO say = proxyBean.say(reqDTO);
//            System.out.println("response:" + say.getCard());
//
//
//            Thread.sleep(50000);
//             reqDTO=new ReqDTO();
//            reqDTO.setName("test wait 50s");
//            reqDTO.setAge(13);
//             say = proxyBean.say(reqDTO);
//            System.out.println("response:" + say.getCard());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
